<?php

namespace App\Models\Activity;

use App\Models\BaseModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProcessV2Model;
use App\Models\Shop\ShopModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class ActivityShopJoinModel extends BaseModel
{
    use HasFactory, KfUid, KfSoftDeletes;

    protected $table = "activity_shop_join";

    protected $fillable = [
        'isDeleted',
    ];

    /**
     * 参与状态
     */
    const STATUS_0    = 0;
    const STATUS_1    = 1;
    const STATUS_2    = 2;
    const STATUS_3    = 3;
    const STATUS_99    = 99; // db中没有这个值

    const STATUS_TEXT = [
        self::STATUS_0 => "已生成",
        self::STATUS_1 => "待审核",
        self::STATUS_2 => "已驳回",
        self::STATUS_3 => "已完成",
        self::STATUS_99 => "已删除",
    ];

    // status优先级
    const STATUS_PRIORITY = [
        self::STATUS_3,
        self::STATUS_1,
        self::STATUS_2,
        self::STATUS_0,
    ];

    /**
     * 参与状态
     */
    const FEEDBACK_STATUS_0    = 0;
    const FEEDBACK_STATUS_1    = 1;
    const FEEDBACK_STATUS_TEXT = [
        self::FEEDBACK_STATUS_0 => "待回传",
        self::FEEDBACK_STATUS_1 => "已回传",
    ];

    /**
     * @return HasOne
     */
    public function cruiseOrderRel(): HasOne
    {
        return $this->hasOne(CruiseOrderModel::class, "id", "orderNo");
    }

    public function shopRel(): HasOne
    {
        return $this->hasOne(ShopModel::class, 'id', 'shopId');
    }

    public function cruiseOrderProcessV2Rel(): HasMany
    {
        return $this->hasMany(CruiseOrderProcessV2Model::class, 'cruiseOrderId', 'orderNo');
    }

    /**
     * @return HasOne
     */
    public function activityRel(): HasOne
    {
        return $this->hasOne(ActivityModel::class, "id", "activityId");
    }

    /**
     * @return HasOne
     */
    public function activityActionRel(): HasOne
    {
        return $this->hasOne(ActivityActionModel::class, "id", "activityActionId");
    }


    /**
     * @return HasOne
     */
    public function shopDetailRel(): HasOne
    {
        return $this->hasOne(ShopModel::class, "id", "shopId");
    }

    /**
     * @return HasMany
     */
    public function joinFeedbackRel(): HasMany
    {
        return $this->hasMany(ActivityShopJoinFeedbackModel::class, "shopJoinId", "id");
    }


    /**
     * 获取门店参与状态
     * @param int $activityId
     * @param int $shopId
     * @param int $activityActionId
     * @param int $actionType
     * @param int $status
     * @param int $feedbackStatus
     */
    public static function getAppliedStatus(
        int $activityId,
        int $shopId,
        int $activityActionId = 0,
        int $actionType = 0,
        int $status = 0,
        int $feedbackStatus = 0
    ) {
        return self::query()
                   ->where('activityId', $activityId)
                   ->where('shopId', $shopId)
                   ->when(
                       $activityActionId, function ($query) use ($activityActionId) {
                       $query->where('activityActionId', $activityActionId);
                   }
                   )
                   ->when(
                       $actionType, function ($query) use ($actionType) {
                       $query->where('actionType', $actionType);
                   }
                   )
                   ->when(
                       $status, function ($query) use ($status) {
                       $query->where('status', $status);
                   }
                   )
                   ->when(
                       $feedbackStatus, function ($query) use ($feedbackStatus) {
                       $query->where('feedbackStatus', $feedbackStatus);
                   }
                   )
                   ->first();
    }

    /**
     * 获取指定类型参与行记录
     *
     * @param int $shopId
     * @param int $activityId
     * @param int $actionType
     * @return Builder|Model|object|null
     */
    public static function getActionTypeJoinRow(int $shopId, int $activityId, int $actionType)
    {
        return self::query()
                   ->where('activityId', $activityId)
                   ->where('shopId', $shopId)
                   ->where("actionType", $actionType)
                   ->first();
    }
}
